package com.thx.resource.dao;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.thx.common.orm.PaginationDao;
import com.thx.resource.model.Resource;

@Repository
public class ResourceDao extends PaginationDao<Resource> {
	
	
	public List<Resource> getAll(String code){
		String hql = "from Resource where code like '"+code+"%'";
		return createQuery(hql).list();
	}

	public List<Resource> getByParentId(String parentId) {
		String hql = "from Resource ";
		if(StringUtils.isBlank(parentId)){
			hql +=" where parentId is null";
		}else{
			hql +=" where parentId ='"+parentId+"'";
		}
		hql += " order by orderSeq";
		return createQuery(hql).list();
	}

	public Resource findByUrl(String url) {
		String hql = "from Resource where url=?";
		List<Resource> list = createQuery(hql,url).list();
		if(list!=null&&list.size()>0){
			return list.get(0);
		}
		return null;
	}

	public Resource findByCode(String code) {
		String hql = "from Resource where code=?";
		List<Resource> list = createQuery(hql,code).list();
		if(list!=null&&list.size()>0){
			return list.get(0);
		}
		return null;
	}
}
